package com.alibaba.aone.casestudy.common.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.aone.casestudy.common.entity.Meta;
import com.alibaba.aone.casestudy.service.MetaService;
import com.alibaba.aone.casestudy.service.impl.MetaServiceImpl;

/**
 * 类MetaUtil.java的实现描述：TODO 类实现描述
 * 
 * @author yongchun.chengyc 2011-5-4 下午12:11:56
 */
public class MetaUtil {

	public static List<Map> getMetaMap(List<Meta> metas) {

		List<Map> list = new ArrayList<Map>();

		String category = null;
		int key = 0;
		String value = null;
		MetaKey metaKey = null;
		Map<MetaKey, String> map = new HashMap<MetaKey, String>();

		for (Meta meta : metas) {
			category = meta.getCategory();
			key = meta.getKey();

			metaKey = new MetaKey(category, key);

			value = meta.getValue();
			map.put(metaKey, value);
			list.add(map);
		}
		return list;
	}

	public String getMetaValue(MetaService service, MetaKey key) {
		String value = null;
		List<Meta> metas = service.allMetas();
		List<Map> metaMaps = MetaUtil.getMetaMap(metas);

		for (Map meta : metaMaps) {
			value = (String) meta.get(key);

		}
		return value;
	}

	public String getValue(MetaService service, String category, int key) {

		MetaKey metaKey = new MetaKey(category, key);
		return getMetaValue(service, metaKey);

	}

}
